package com.aim.game;

import com.aim.util.extractors.DoubleValueExtractor;
import com.aim.util.ListUtil;
import org.apache.commons.math3.stat.StatUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: User
 * Date: 5/7/14
 * Time: 11:19 PM
 * To change this template use File | Settings | File Templates.
 */
public class GameResultCollection {

    List<GameResult> resultList = new ArrayList<GameResult>();

    public void add(GameResult result){
        resultList.add(result);
    }

    public double averageCost(){
        double avgCost = 0;
        for (GameResult result : resultList) {
            avgCost += result.totalCost;
        }
        return avgCost / resultList.size();
    }

    public double averageTimeCost(){
        double[] timesCost = ListUtil.toDoubleArray(resultList, new DoubleValueExtractor<GameResult>() {
            @Override
            public Double getValue(GameResult gameResult) {
                return gameResult.totalTimeCost;
            }
        });
        return StatUtils.mean(timesCost);
    }

    private long gameLength(){
        return resultList.get(0).gameLength;
    }

    public double getFinalScore(){
        return (gameLength() - averageTimeCost()) * getScorePerformance() -  averageCost();
        //return 1 - (averageTimeCost()) * getScorePerformance() -  averageCost();
    }

    private double getScorePerformance() {
        return resultList.get(0).scorePerformance;
    }


    public double[] getTotalCostArray(){
        return ListUtil.toDoubleArray(resultList, new DoubleValueExtractor<GameResult>() {
            @Override
            public Double getValue(GameResult gameResult) {
                return gameResult.totalCost;
            }
        });
    }

    public double getAverageRepairCount(){
        double[] counts = ListUtil.toDoubleArray(resultList, new DoubleValueExtractor<GameResult>() {
            @Override
            public Double getValue(GameResult gameResult) {
                return gameResult.repairCount;
            }
        }
        );
        return StatUtils.mean(counts);
    }

    public double getAverageMaCountMaintenance(){
        double[] counts = ListUtil.toDoubleArray(resultList, new DoubleValueExtractor<GameResult>() {
            @Override
            public Double getValue(GameResult gameResult) {
                return gameResult.maintenanceCount;
            }
        }
        );
        return StatUtils.mean(counts);
    }


}
